home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
embedded
/
develop
/
ad2mac.arc
/
AD2MAC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-08-04
|
10KB
|
325 lines
/**************************************************************************/
/* */
/* */
/* FILENAME: AD2MAC.C 1.0 */
/* */
/* */
/* DESCRIPTION: */
/* Retrieve the symbol name and value from the assembly */
/* language listing of symbol table which produced by the */
/* 2500AD assembler. */
/* */
/* This program will create an output macro file which can */
/* be used to obtain the symbol information for symbolic */
/* debug support on HDS-300 Hardware Development Station. */
/* It also has an output format option for use with the */
/* Motorola CSIC Development System (CDS) for the same */
/* purpose. */
/* */
/* */
/* PROGRAMMER: FAI LEE TUNG STEINBERG */
/* */
/* DATE: AUG. 04, 1989 */
/* */
/* INPUT FILE: 2500AD assembler listing */
/* */
/* OUTPUT FILE: Symbolic file in HDS-300 format or */
/* in CDS format */
/* */
/**************************************************************************/
/*------------------------------------------------------------------------*/
/* I N C L U D E AND D E F I N E F I L E S */
/*------------------------------------------------------------------------*/
#include <stdio.h> /* Stream functions */
#include <ctype.h> /* C macros and constance defines */
#include <string.h> /* String functions */
/*------------------------------------------------------------------------*/
#define HDRFLD "Defined" /* define the table heading */
#define ENDFLD "Lines" /* define the table ending */
#define VALID1 "._" /* define the valid name */
#define VALID2 "._$" /* define the valid name */
#define DELIMITER " ;=" /* define the delimiter value */
/*------------------------------------------------------------------------*/
#define MAX 132 /* define max line length */
#define LENGTH 8 /* define max symbol length */
#define NAMELEN 31 /* define max name length */
#define LIMIT 6 /* valid symbolic value line */
#define FILELEN 12 /* define the filename length */
#define FALSE (0) /* Function FALSE value */
#define TRUE (1) /* Function TRUE value */
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
/* G L O B A L V A R I A B L E */
/*------------------------------------------------------------------------*/
FILE *mf, /* File pointers */
*of;
typedef short BOOLEAN; /* bi-valued (TRUE/FALSE) */
/*------------------------------------------------------------------------*/
/* F U N C T I O N D E C L A R A T I O N S */
/*------------------------------------------------------------------------*/
void putformat();
char *storelength(char *, int);
char *storename(char *, int);
/*------------------------------------------------------------------------*/
/* M A I N */
/*------------------------------------------------------------------------*/
main(argc, argv) /* AD2MAC main() function */
int argc; /* Number of command line args */
char *argv[]; /* Vector of command line args */
{
char *mac = ".mac", /* Macro file extension */
*rtype = "r", /* opens to reading the file only */
*wtype = "w", /* opens to writing the file only */
*filein, /* store input filename */
fileout[FILELEN], /* store output filename */
string[MAX], /* store a line string */
temp[MAX], /* temporary string storage */
*namelegth, /* truncate the symbol name */
*name, /* store the valid symbol name */
val[LENGTH], /* store symbol value */
*format, /* store format command */
*token, /* string token */
*fptr; /* file pointers */
int i,j,m,n, /* initial counter variables */
legth, /* initial counter variables */
char_ct, /* character counter */
c, /* option flags variables */
form_flag,
opt_flag,
use_ct; /* reference counter */
BOOLEAN EOF_flag; /* end of file flag */
EOF_flag = FALSE; /* initialization */
form_flag = 0;
opt_flag = 0;
/*------------------------------------------------------------------------*/
/* Function for all options on command line */
/*------------------------------------------------------------------------*/
strcpy(filein, argv[argc-(argc-1)]);
mf=fopen(filein, rtype);
*argv++;
while ( (c = getopt(argc, argv, "iIcC")) != -1 )
switch(c)
{
case 'c':
case 'C':
form_flag = 1;
break;
case 'i':
case 'I':
opt_flag = 1;
break;
default:
break;
}
for (i=0; *filein != '.'; i++)
{
fileout[i] = *filein++;
}
fileout[i] = '\0';
strcat(fileout, mac);
of=fopen(fileout, wtype);
/*------------------------------------------------------------------------*/
/* Read a line to the buffer, begin to locate the symbol table */
/* heading. If found, begin to extract the valid symbol name and */
/* value from the table. If not found, read the next line */
/*------------------------------------------------------------------------*/
while ( (fgets(string, MAX, mf) != NULL) )
{
fptr=string;
token = strtok(fptr,DELIMITER);
if ( strcmp(token,HDRFLD) == 0) {
/*--------------------------------------------------------------------*/
/* Symbol table is located, read a line to the buffer until EOF */
/* flag is set. Extract the obtainable address symbol name and */
/* value only. */
/*--------------------------------------------------------------------*/
while ( !EOF_flag ) {
fgets(string, MAX, mf);
fptr=string;
n = 0;
token = strtok(fptr,DELIMITER);
if ( isdigit(*token) ) {
token = strtok(NULL,DELIMITER);
n = 1;
/*---------------------------------------------------------------------*/
/* The symbol name is truncated and the invalid characters are */
/* replaced with underscore (_) */
/*---------------------------------------------------------------------*/
if (form_flag == 1)
{
namelegth = storelength(token,NAMELEN);
legth = NAMELEN;
}
else
{
namelegth = storelength(token,LENGTH);
legth = LENGTH;
}
name = storename(namelegth,legth);
/*---------------------------------------------------------------------*/
/* The leading zeros of the symbol value are truncated */
/*---------------------------------------------------------------------*/
token = strtok(NULL,DELIMITER);
j = 0;
m = 0;
while ( isxdigit(*token) )
{
if ( *token != '0' )
while ( isxdigit(*token) )
{
val[j] = *token;
j++;
token++;
m++;
}
else
token++;
}
val[j] = '\0';
/*---------------------------------------------------------------------*/
/* Define the symbol name and value is used or not */
/*---------------------------------------------------------------------*/
token = strtok(NULL,DELIMITER);
use_ct = 0;
if ( isdigit(*token) && (*token != NULL) )
{
use_ct++;
}
/*------------------------------------------------------------------------*/
/* Print Options: */
/* HDS-300 Listing format */
/* CDS Listing format */
/* Used Symbols only */
/*------------------------------------------------------------------------*/
if ( (n > 0) && (m > 0) )
{
switch(form_flag)
{
case 0:
format = " @LS";
putformat(opt_flag,use_ct,format,name,val);
break;
case 1:
format = "DEFINE";
putformat(opt_flag,use_ct,format,name,val);
break;
default:
break;
}
}
}
else
{
token = strtok(fptr,DELIMITER);
if ( strcmp(token,ENDFLD) == 0 )
{
EOF_flag = TRUE;
}
}
}
}
}
if (feof(mf))
printf("End of file\n");
else
printf("Error reading file!!!\n");
printf("***** MACRO LISTING FILENAME ==> %s\n\n", fileout);
fclose(mf);
fclose(of);
}
char *storelength(val,maxlen)
char *val;
int maxlen;
{
int ch_ct;
char name[MAX];
char *ptr;
ch_ct = 0;
while ( *val != NULL)
if ( ch_ct < maxlen)
{
name[ch_ct] = *val;
val++;
ch_ct++;
}
else
val++;
name[ch_ct] = '\0';
ptr=name;
return(ptr);
}
char *storename(ch,maxlen)
char *ch;
int maxlen;
{
int j;
char name[MAX];
char *ptr;
j = 0;
name[j] = *ch;
while ( (j < maxlen) && (!isspace(name[j])) )
{
if (j == 0)
{
if ( !((isalpha(name[j])) || (strchr(VALID1, name[j]) != NULL)) )
{
name[j] = '_';
j++;
}
else
{
j++;
}
}
else if ( !((isalnum(name[j])) || (strchr(VALID2,name[j]) != NULL)) )
{
name[j] = '_';
j++;
}
else
{
j++;
}
}
name[j] = '\0';
ptr = name;
return (ptr);
}
void
putformat(opt,use_ct,format,name,val)
int *val, opt, use_ct;
char *name;
{
if ( opt == 0 )
{
printf("%s %s %s\n", format, name, val);
fprintf(of, "%s %s %s\n", format, name, val);
}
else if ( use_ct >= 1 )
{
printf("%s %s %s\n", format, name, val);
fprintf(of, "%s %s %s\n", format, name, val);
}
}